package com.bsoft.productor;

import com.bsoft.message.Demo07Message;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Demo07Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public  void  syncSend(Integer id ,Integer delay){
        //创建消息
        Demo07Message message = new Demo07Message();
        message.setId(id);
        //同步发送消息
        rabbitTemplate.convertAndSend(Demo07Message.EXCHANGE, Demo07Message.ROUTING_KEY, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息的TTL 过期时间
                if (delay!=null && delay>0){
                    message.getMessageProperties().setExpiration(String.valueOf(delay));
                }
                return message;
            }
        });

    }
}
